#include "dishrecommendunit.h"
#include "ui_dishrecommendunit.h"
#include "dishitem.h"
#include <QToolButton>
#include <dishdetail.h>
DishRecommendUnit::DishRecommendUnit(QWidget *parent,QString title,QList<Dish>* dishList) :
    QWidget(parent),
    ui(new Ui::DishRecommendUnit),
    title(title),
    dishList(dishList)
{
    ui->setupUi(this);
    ui->lable_title->setText(ui->lable_title->text()+title);
    for(int i = ui->stackedWidget->count(); i >= 0; i--)
    {
        QWidget* widget = ui->stackedWidget->widget(i);
        ui->stackedWidget->removeWidget(widget);
        widget->deleteLater();
    }
    for(int i=0;i<dishList->count();i++)
    {
        Dish aDish  = dishList->at(i);
        DishItem* dishItem = new DishItem(this,aDish.id,aDish.name,aDish.material,aDish.detail,aDish.pic,aDish.type);
        ui->stackedWidget->insertWidget(i,dishItem);
    }
    int totalIndex = ui->stackedWidget->count();
//    qDebug()<<totalIndex;
    ui->stackedWidget->setCurrentIndex(0);
    connect(ui->btn_after,&QToolButton::clicked,[=](){
        int current = ui->stackedWidget->currentIndex();
        current++;
        if(current==totalIndex)
        {
            current = 0;
        }
//        qDebug()<<current;
        ui->stackedWidget->setCurrentIndex(current);
    });
    connect(ui->btn_before,&QToolButton::clicked,[=](){
        int current = ui->stackedWidget->currentIndex();
        current--;
        if(current==-1)
        {
            current = totalIndex-1;
        }
//        qDebug()<<current;
        ui->stackedWidget->setCurrentIndex(current);
    });


}

DishRecommendUnit::~DishRecommendUnit()
{
    delete ui;
}
